unit CrGame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, StdCtrls, Spin, Plansza;

type
  TForm3 = class(TForm)
    ListBox1: TListBox;
    Label1: TLabel;
    Button1: TButton;
    SpinEdit1: TSpinEdit;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    ListBox2: TListBox;
    prtCtrl: TSpinEdit;
    Label3: TLabel;
    procedure ListBox1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;
  Main:String;

implementation

uses Unit1, MMenu;

{$R *.dfm}

procedure TForm3.ListBox1Click(Sender: TObject);
var Temp:TPlansza;
begin
  If ListBox1.ItemIndex<>-1 then
  begin
    Temp:=TPlansza.Create;
    Temp.LoadFromFile(Main+ListBox2.Items[ListBox1.ItemIndex]);
    Label1.Caption:='Max graczy: '+IntToStr(Temp.MaxPlayerz);
    SpinEdit1.MaxValue:=Temp.MaxPlayerz;
    SpinEdit1.MinValue:=1;
    SpinEdit1.Value:=2;
    LoadMap:=ListBox2.Items[ListBox1.ItemIndex];
    Button1.Enabled:=True;
  end;
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  Form1.Server.Port:=prtCtrl.Value;
  Form1.Client.Port:=prtCtrl.Value;
  Form1.SpinEdit2.Value:=SpinEdit1.Value;
  Form1.Button4Click(Sender);
  Form1.Show;
  InitArrays;
  Close;
  Form2.Hide;
end;

procedure ScanDirectory(Dir:String);
var
  SearchRec: TSearchRec;
  FileMask:String;
  S,F:String;
begin
  FileMask:='*.*';
//  if dir <> '' then ChDir(dir);
  ChDir(Dir);
  if FindFirst(filemask, faAnyFile, SearchRec) = 0 then
    repeat
      if (ExtractFileExt(SearchRec.Name)='.map') then
        begin
          Form3.ListBox1.Items.Add(SearchRec.Name);
          S:=Dir+'\'+SearchRec.Name;
          F:=Copy(S, Length(Main)+1,Length(S)-Length(Main)+1);
          Form3.ListBox2.Items.Add(F);
//          Form3.ListBox2.Items.Add(Dir+'\'+SearchRec.Name);
        end;
      if ((SearchRec.Attr and faDirectory) <> 0) and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
      begin
        ScanDirectory(Dir+'\'+SearchRec.Name);
      end;
    until (FindNext(SearchRec) <> 0);
  SysUtils.FindClose(SearchRec);
  // Directory up
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  Main:=ExtractFilePath(Application.ExeName)+'\data\maps';
  ScanDirectory(Main);
end;

procedure TForm3.SpinEdit1Change(Sender: TObject);
begin
  If SpinEdit1.Value<2 then SpinEdit1.Value:=2;
end;

procedure TForm3.ListBox1DblClick(Sender: TObject);
begin
  If ListBox1.ItemIndex<>-1 then
    Button1Click(nil);
end;

end.
